package com.dingding.order.core.common.distance.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.City;
import com.dingding.common.core.beans.special.SpecialDrivingRecord;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.commonservice.common.dto.company.ReqServiceCity;
import com.dingding.facade.commonservice.common.dto.distance.ReqMileageCostDTO;
import com.dingding.facade.commonservice.common.dto.distance.ResMileageCostDTO;
import com.dingding.facade.commonservice.facade.city.ICityFacade;
import com.dingding.facade.commonservice.facade.distance.ICalculateDistanceFacade;
import com.dingding.facade.special.ISpecialFacade;
import com.dingding.facade.special.trasfer.calc_price.ResCalculationAmountDTO;
import com.dingding.facade.user.common.dto.special.driver.ReqSpecialDrivingRecord;
import com.dingding.facade.user.common.dto.special.driver.ResOrderMileageCost;
import com.dingding.facade.user.facade.special.driver.ISpecialDrivingRecordFacade;
import com.dingding.order.common.dto.distance.MileageCostDTO;
import com.dingding.order.common.dto.distance.RealCalculationDTO;
import com.dingding.order.common.dto.distance.ReqCalculationDTO;
import com.dingding.order.common.dto.distance.ReqRealTimeCalculationDTO;
import com.dingding.order.common.dto.distance.ResCalculationDTO;
import com.dingding.order.common.dto.distance.ResRealTimeCalculationDTO;
import com.dingding.order.common.dto.rule.ReqValuationRule;
import com.dingding.order.common.dto.rule.ReqValuationRuleByVehicleId;
import com.dingding.order.common.dto.rule.ResValuationRule;
import com.dingding.order.common.dto.special.ReqGetSpecialOrdertByIdDTO;
import com.dingding.order.common.util.MapUtil;
import com.dingding.order.common.util.dto.OrderMileageDTO;
import com.dingding.order.core.common.distance.service.ICalculateService;
import com.dingding.order.facade.common.rule.IValuationRuleFacade;
import com.dingding.order.facade.special.IUserSpecialOrdertFacade;

/**
 * 
 * @ClassName: ICalculateService 
 * @Description: 计算距离价格业务逻辑实现类
 * @author YHQ
 * @date 2016年12月7日 下午1:42:26
 * @since V2.5.7
 * 
 */
@Component("calculateService")
public class CalculateServiceImpl implements ICalculateService{

	@Resource(name = "openCityFacade1")
	private ICityFacade cityFacade;
	
	@Resource(name = "valuationRuleFacade")
	private IValuationRuleFacade valuationRuleFacade;
	
	@Resource(name = "calculateDistanceFacade1")
	private ICalculateDistanceFacade calculateDistanceFacade;
	
	@Resource(name = "userSpecialOrdertFacade")
	private IUserSpecialOrdertFacade SpecialOrdertFacade;
	
	@Resource(name = "specialDrivingRecordFacade")
	private ISpecialDrivingRecordFacade specialDrivingRecordFacade;
	
	@Resource(name = "specialFacade")
	private ISpecialFacade specialFacade;
	
	/**
	 * 
	 * @Title: calculationMileageCost 
	 * @Description: 计算距离、费用(专车、出租车使用)
	 * @param reqCalculationDTO 客户端请求参数 
	 * @author YHQ 
	 * @return ResponseEntity<ResCalculationDTO> 响应给客户端的数据Bean
	 * @since V2.5.7
	 * 
	 */
	@Override
	public ResponseEntity<ResCalculationDTO> calculationMileageCost(ReqCalculationDTO reqCalculationDTO,ResponseEntity<ResCalculationDTO> responseEntity) {
		
		// 判断客户端请求参数是否为空
		if(reqCalculationDTO == null || !ToolUtil.validateParam(reqCalculationDTO)){
			
			responseEntity.setCode(HttpStatus.SC_FORBIDDEN);
			responseEntity.setErrorMessage("客户端请求参数为空");
			responseEntity.setPromptMessage("客户端请求数据为空");
			return null;
		}
		
		// 补全客户端请求参数(订单里程、城市编码)
		this.completionParam(reqCalculationDTO);
		
		// 创建计算距离价格客户端响应Bean
		ResCalculationDTO resCalculationDTO = new ResCalculationDTO();
		
		// 根据城市Code码判断该城市是否开通   true:开通  false:未开通
		String cityId = this.isServiceCity(reqCalculationDTO, resCalculationDTO,responseEntity);
		
		if(ToolUtil.isBlank(cityId)){
			
			responseEntity.setCode(HttpStatus.SC_FORBIDDEN);
			responseEntity.setErrorMessage("该城市暂未开通");
			responseEntity.setPromptMessage("该城市暂未开通");
			
			return responseEntity;
		}
		
		// 获取开通城市的车辆类型计价规则列表
		List<ResValuationRule> chargeRules = this.getChargeRule(cityId);
		
		// 计算距离价格，封装响应Bean
		this.calculationCost(reqCalculationDTO, resCalculationDTO, chargeRules);
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setResponseBody(resCalculationDTO);
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: completionParam 
	 * @Description: 自动补全客户端请求参数
	 * @param reqCalculationDTO  客户端请求参数
	 * @author YHQ 
	 * @since V2.5.7
	 * @return void    返回类型
	 * 
	 */
	public void completionParam(ReqCalculationDTO reqCalculationDTO){
		
		// 订单里程
		OrderMileageDTO orderMileageDTO = MapUtil.getSingleDistance(reqCalculationDTO.getStartLongitude(), reqCalculationDTO.getStartLatitude(), reqCalculationDTO.getCurrentLongitude(), reqCalculationDTO.getCurrentLatitude());
	
		if(orderMileageDTO != null){
			
			if(orderMileageDTO.getDistance() != null){
				// 订单里程(公里)
				reqCalculationDTO.setDistance(new BigDecimal(orderMileageDTO.getDistance()).divide(new BigDecimal(1000)));
			}else{
				reqCalculationDTO.setDistance(new BigDecimal(0));
			}
			
			if(orderMileageDTO.getDuration() != null){
				// 订单时长
				reqCalculationDTO.setDuration(new BigDecimal(orderMileageDTO.getDuration()).divide(new BigDecimal(60)));
			}else{
				// 订单时长
				reqCalculationDTO.setDuration(new BigDecimal(0));
			}
			
		}else{
			// 订单里程(公里)
			reqCalculationDTO.setDistance(new BigDecimal(0));
			// 订单时长
			reqCalculationDTO.setDuration(new BigDecimal(0));
		}
		
		if(ToolUtil.isBlank(reqCalculationDTO.getCityCode())){
			reqCalculationDTO.setCityCode(orderMileageDTO.getCitycode());
		}
		
	}
	
	/**
	 * 
	 * @Title: isServiceCity 
	 * @Description: 判断是否为服务城市
	 * @param reqCalculationDTO 客户端请求参数
	 * @param resCalculationDTO 响应Bean
	 * @author YHQ 
	 * @return String 城市Id
	 * 
	 */
	public String isServiceCity(ReqCalculationDTO reqCalculationDTO,ResCalculationDTO resCalculationDTO,ResponseEntity<ResCalculationDTO> responseEntity){
		
		// 该城市是否开通请求Bean
		ReqServiceCity reqServiceCity = new ReqServiceCity();
		
		// 城市编号 
		reqServiceCity.setCityNo(reqCalculationDTO.getCityCode());
		
		// 订单里程
		resCalculationDTO.setDistance(String.valueOf(reqCalculationDTO.getDistance().setScale(1,BigDecimal.ROUND_UP)));
		
		// 是否允许发单(1:不允许   2:允许)
		if(reqCalculationDTO.getDistance().compareTo(new BigDecimal(1)) < 0){
			resCalculationDTO.setIsCondition(1);
			
			responseEntity.setCode(HttpStatus.SC_FORBIDDEN);
			responseEntity.setErrorMessage("距离过近，请步行前往");
			responseEntity.setPromptMessage("距离过近，请步行前往");
			
		}else{
			resCalculationDTO.setIsCondition(2);
		}
		
		// 获取是否为服务城市接口
	 	City city = cityFacade.isServiceCity(reqServiceCity);
		
	 	// 如果为空则默认为未开通
	 	if(city == null || !ToolUtil.validateParam(city)){
	 		
	 		// 是否是服务城市
	 		resCalculationDTO.setIsServiceCity(1);
	 		// 不是服务城市，无法发布订单
	 		resCalculationDTO.setIsCondition(1);
	 		
			responseEntity.setCode(HttpStatus.SC_FORBIDDEN);
			responseEntity.setErrorMessage("该城市暂未开通,敬请期待");
			responseEntity.setPromptMessage("该城市暂未开通,敬请期待");
	 		
	 		return null;
	 	}else{
	 		
	 		// 是否是服务城市
	 		resCalculationDTO.setIsServiceCity(city.getIsServiceCity());
	 		
	 		return city.getCityId();
	 	}
	}
	
	/**
	 * 
	 * @Title: getChargeRule 
	 * @Description: 获取计价规则列表
	 * @param cityId 城市Id
	 * @author YHQ 
	 * @since V2.5.7
	 * @return List<ChargeRule>    返回类型
	 * 
	 */
	public List<ResValuationRule> getChargeRule(String cityId){
		
		if(ToolUtil.isBlank(cityId)){
			return null;
		}
		
		// 创建计价规则请求Bean
		ReqValuationRule reqValuationRule = new ReqValuationRule();
		
		// 设置城市Id
		reqValuationRule.setCityId(cityId);
		 
		// 获取计价规则列表
		return valuationRuleFacade.getChargeRuleList(reqValuationRule);
	}
	
	/**
	 * 
	 * @Title: calculationCost 
	 * @Description: 计算里程费用
	 * @param chargeRules 车型计价规则
	 * @author YHQ 
	 * @since V2.5.7
	 * @return void    返回类型
	 * 
	 */
	public void calculationCost(ReqCalculationDTO reqCalculationDTO,ResCalculationDTO resCalculationDTO,List<ResValuationRule> chargeRules){
		
		// 车辆类型、费用列表
		List<MileageCostDTO> typePriceList = new ArrayList<MileageCostDTO>();	
		
		if(chargeRules != null && chargeRules.size() > 0){
			
			for (ResValuationRule chargeRule : chargeRules) {
				
				// 响应Bean
				MileageCostDTO mileageCostDTO = new MileageCostDTO();
				// 创建里程费用请求Bean
				ReqMileageCostDTO reqMileageCostDTO = new ReqMileageCostDTO();
				
				// common项目所需Bean
				com.dingding.facade.commonservice.common.dto.rule.ResValuationRule valuationRule = new com.dingding.facade.commonservice.common.dto.rule.ResValuationRule();
				
				// 订单里程
				reqMileageCostDTO.setOrderMileage(reqCalculationDTO.getDistance());
				// 设置计算类型 1:计算距离价格   2:实时计算距离价格
				reqMileageCostDTO.setCalculationType(1);
				// 订单时长
				reqMileageCostDTO.setDuration(reqCalculationDTO.getDuration());
				
				try {
					// 将order项目中的ResValuationRule 赋值给 ResValuationRule
					BeanUtils.copyProperties(valuationRule, chargeRule);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				
				// 计价规则
				reqMileageCostDTO.setChargeRule(valuationRule);
				
				ResMileageCostDTO resMileageCostDTO = calculateDistanceFacade.calculationMileageCost(reqMileageCostDTO);
				
				mileageCostDTO.setTotalMoney(String.valueOf(resMileageCostDTO.getTotalMoney().setScale(1,BigDecimal.ROUND_UP)));
				mileageCostDTO.setVehicleLevelId(chargeRule.getVehicleLevelId());
				mileageCostDTO.setVehicleLevel(chargeRule.getVehicleLevel());
				
				typePriceList.add(mileageCostDTO);
			}
			
			resCalculationDTO.setTypePriceList(typePriceList);
		}
		
	}

	/**
	 * 
	 * @Title: realTimeCalculation 
	 * @Description: 实时计算距离价格 
	 * @param reqRealTimeCalculationDTO 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.7
	 * @return ResRealTimeCalculationDTO  实时计算距离价格 响应Bean
	 * 
	 */
	@Override
	public ResRealTimeCalculationDTO realTimeCalculation(ReqRealTimeCalculationDTO reqRealTimeCalculationDTO) {
		
		// 判断客户端请求参数是否为空
		if(reqRealTimeCalculationDTO == null || !ToolUtil.validateParam(reqRealTimeCalculationDTO)){
			return null;
		}
		
		// 获取订单信息
		UserspecialorderT specialOrdert = this.getUserSpecialOrdert(reqRealTimeCalculationDTO.getOid());
		
		// 订单不存在
		if(specialOrdert == null){
			return null;
		}
		
		// 获取车主行车记录表最新一条记录
		SpecialDrivingRecord specialDrivingRecord = this.getSpecialDrivingRecord(reqRealTimeCalculationDTO);
		
		// 计算距离、金额和时长
		RealCalculationDTO realCalculationDTO = this.realCalculation(reqRealTimeCalculationDTO,specialOrdert, specialDrivingRecord);
		
		// 添加一条新的车主行车记录
		this.saveSpecialDrivingRecord(reqRealTimeCalculationDTO, realCalculationDTO);
		
		ResRealTimeCalculationDTO orderMileageCost = this.getOrderMileageCost(reqRealTimeCalculationDTO);
		
		if(realCalculationDTO.getAmount().compareTo(orderMileageCost.getAmount()) > 0){
			orderMileageCost.setAmount(realCalculationDTO.getAmount().setScale(1,BigDecimal.ROUND_UP));
		}
		
		// 同步距离价格到订单中
		specialOrdert.setDistance(orderMileageCost.getDistance());
		specialOrdert.setDuration(orderMileageCost.getDuration());
		specialOrdert.setTotalMoney(orderMileageCost.getAmount());
		
		SpecialOrdertFacade.updateSpecialOrder(specialOrdert);
		
		//推送给乘客实时价格信息
		ResCalculationAmountDTO resCalculationAmountDTO = new ResCalculationAmountDTO();
		resCalculationAmountDTO.setAmount(specialOrdert.getTotalMoney());
		resCalculationAmountDTO.setDistance(specialOrdert.getDistance());
		resCalculationAmountDTO.setDuration(specialOrdert.getDuration());
		resCalculationAmountDTO.setPublishType(3);
		resCalculationAmountDTO.setOid(specialOrdert.getSpecialOrderId());
		
		this.specialFacade.pushCustomerCalcPrice(specialOrdert.getPlaceOrderUserId(), resCalculationAmountDTO);
		
		return orderMileageCost;
	}

	
	/**
	 * 
	 * @Title: getUserSpecialOrdert 
	 * @Description: 获取订单信息
	 * @param orderId 订单Id
	 * @author YHQ 
	 * @since V2.5.7
	 * @return UserspecialorderT    返回类型
	 * 
	 */
	public UserspecialorderT getUserSpecialOrdert(String orderId){
		
		// 创建获取订单请求Bean
		ReqGetSpecialOrdertByIdDTO reqGetSpecialOrdertByIdDTO = new ReqGetSpecialOrdertByIdDTO();
		
		// 订单Id
		reqGetSpecialOrdertByIdDTO.setOrderId(orderId);
		
		return SpecialOrdertFacade.getSpecialOrdertById(reqGetSpecialOrdertByIdDTO);
	}
	
	/**
	 * 
	 * @Title: getSpecialDrivingRecord 
	 * @Description: 获取最近一条车辆行车记录 
	 * @param reqRealTimeCalculationDTO 客户端请求参数
	 * @author YHQ 
	 * @since V2.5.7
	 * @return SpecialDrivingRecord    返回类型
	 * 
	 */
	public SpecialDrivingRecord getSpecialDrivingRecord(ReqRealTimeCalculationDTO reqRealTimeCalculationDTO){
		
		// 获取车辆行车记录最新一条记录
		ReqSpecialDrivingRecord reqSpecialDrivingRecord = new ReqSpecialDrivingRecord();
		
		// 订单Id
		reqSpecialDrivingRecord.setOrderId(reqRealTimeCalculationDTO.getOid());
		// 用户Id
		reqSpecialDrivingRecord.setUserId(reqRealTimeCalculationDTO.getUserId());
		
		return specialDrivingRecordFacade.getSpecialDrivingRecored(reqSpecialDrivingRecord);
	}
	
	/**
	 * 
	 * @Title: realCalculation 
	 * @Description: 实时计算 
	 * @param specialDrivingRecord 最近行车距离
	 * @param reqRealTimeCalculationDTO 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.7
	 * @return ResRealTimeCalculationDTO    返回类型
	 * 
	 */
	public RealCalculationDTO realCalculation(ReqRealTimeCalculationDTO reqRealTimeCalculationDTO,UserspecialorderT specialOrdert,SpecialDrivingRecord specialDrivingRecord){
		
		// 创建实时计算响应Bean
		RealCalculationDTO realCalculationDTO = new RealCalculationDTO();
		
		// 当前时间
		realCalculationDTO.setCreatTime(new Date());
		
		if(specialDrivingRecord == null){
			
			// 累计金额
			realCalculationDTO.setAmount(new BigDecimal(0));
			// 累计里程
			realCalculationDTO.setDistance(new BigDecimal(0));
			// 累计时长
			realCalculationDTO.setDuration(new BigDecimal(0));
			
		}else{
			
			// 根据上一个位置的经纬度和当前位置经纬度计算里程、时长等
			OrderMileageDTO orderMileageDTO = MapUtil.getSingleDistance(specialDrivingRecord.getLongitude(), specialDrivingRecord.getLatitude(), reqRealTimeCalculationDTO.getLongitude(), reqRealTimeCalculationDTO.getLatitude());
		
			// 计算时长
			Integer duration = DateFormatUtil.getDateTimeDifferenceBySecond(specialDrivingRecord.getCreateTime(), realCalculationDTO.getCreatTime());
			// 计算类型
			if(reqRealTimeCalculationDTO.getCalculationType() != null){
				realCalculationDTO.setCalculationType(reqRealTimeCalculationDTO.getCalculationType());
			}else{
				realCalculationDTO.setCalculationType(2);
			}
			// 订单里程
			realCalculationDTO.setDistance(new BigDecimal(orderMileageDTO.getDistance()).divide(new BigDecimal(1000),1,BigDecimal.ROUND_UP));
			// 订单时长
			realCalculationDTO.setDuration(new BigDecimal(duration).divide(new BigDecimal(60),1,BigDecimal.ROUND_UP));
			
			// 订单里程费用
			this.calculationCost(specialOrdert, realCalculationDTO);
		}
		
		return realCalculationDTO;
	}
	
	/**
	 * 
	 * @Title: calculationCost 
	 * @Description: 实时计算费用
	 * @param specialOrdert 订单对象
	 * @param resRealTimeCalculationDTO 实时计算距离价格响应Bean
	 * @author YHQ 
	 * @since V2.5.7
	 * @return void    返回类型
	 * 
	 */
	public void calculationCost(UserspecialorderT specialOrdert,RealCalculationDTO realCalculationDTO){
		
		// 计价规则
		com.dingding.facade.commonservice.common.dto.rule.ResValuationRule valuationRule = this.getResValuationRule(specialOrdert);
		
		// 创建计算距离价格请求Bean
		ReqMileageCostDTO reqMileageCostDTO = new ReqMileageCostDTO();
		
		if(getReqMileageCost(valuationRule.getNightStart(), valuationRule.getNightEnd(),realCalculationDTO.getCreatTime())){	// 夜间
			reqMileageCostDTO.setOrderMileage(new BigDecimal(0));
			reqMileageCostDTO.setNightMileage(realCalculationDTO.getDistance());
		}else{	// 白天
			reqMileageCostDTO.setOrderMileage(realCalculationDTO.getDistance());
			reqMileageCostDTO.setNightMileage(new BigDecimal(0));
		}
		
		// 计价规则
		reqMileageCostDTO.setChargeRule(valuationRule);
		
		// 计算类型  1:计算距离价格   2:实时计算距离价格
		reqMileageCostDTO.setCalculationType(realCalculationDTO.getCalculationType());
		// 订单时长
		reqMileageCostDTO.setDuration(realCalculationDTO.getDuration());
		
		// 计算里程费用
		ResMileageCostDTO resMileageCostDTO = calculateDistanceFacade.calculationMileageCost(reqMileageCostDTO);
		
		// 给响应Bean赋值
		realCalculationDTO.setAmount(resMileageCostDTO.getTotalMoney());
	}
	
	/**
	 * 
	 * @Title: getResValuationRule 
	 * @Description: 获取计价规则 
	 * @param specialOrdert 订单信息
	 * @author YHQ 
	 * @return com.dingding.facade.commonservice.common.dto.rule.ResValuationRule    返回类型
	 * 
	 */
	public com.dingding.facade.commonservice.common.dto.rule.ResValuationRule getResValuationRule(UserspecialorderT specialOrdert){
		
		// 创建获取计价规则请求Bean
		ReqValuationRuleByVehicleId reqValuationRuleByVehicleId = new ReqValuationRuleByVehicleId();
		// 车辆等级Id
		reqValuationRuleByVehicleId.setVehicleLevelId(specialOrdert.getVehicleLevelId());
		
		// 获取计价规则
		ResValuationRule resValuationRule = valuationRuleFacade.getChargeRuleByVehicleLevelId(reqValuationRuleByVehicleId);
		
		// 计算距离价格请求Bean
		com.dingding.facade.commonservice.common.dto.rule.ResValuationRule valuationRule = new com.dingding.facade.commonservice.common.dto.rule.ResValuationRule();
		
		try {
			// 将order项目中的ResValuationRule 赋值给 ResValuationRule
			BeanUtils.copyProperties(valuationRule, resValuationRule);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return valuationRule;
	}
	
	/**
	 * 
	 * @Title: getReqMileageCost 
	 * @Description: 判断当前时间属于白天还是晚上
	 * @param nightStart 夜间开始时间
	 * @param nightEnd 夜间结束时间
	 * @author YHQ 
	 * @return true:夜间  false:白天
	 * 
	 */
	public boolean getReqMileageCost(Date nightStart,Date nightEnd,Date creatTime){
		
		// 当天夜间开始时间
		nightStart = DateFormatUtil.getNightTime(creatTime, nightStart);
		
		// 当天夜间结束时间
		nightEnd = DateFormatUtil.getNightTime(creatTime, nightEnd);
		
		// 判断当前时间属于白天还是夜间
		if(nightStart.compareTo(creatTime) > 0 && nightEnd.compareTo(creatTime) < 0){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 
	 * @Title: saveSpecialDrivingRecord 
	 * @Description: 保存一条新的行车记录
	 * @param resRealTimeCalculationDTO 要保存到数据库的行车信息
	 * @author YHQ 
	 * @since V2.5.7
	 * @return boolean    返回类型
	 * 
	 */
	public boolean saveSpecialDrivingRecord(ReqRealTimeCalculationDTO reqRealTimeCalculationDTO,RealCalculationDTO realCalculationDTO){
		
		// 司机行车记录表
		SpecialDrivingRecord specialDrivingRecord = new SpecialDrivingRecord();
		
		// 主键ID
		specialDrivingRecord.setSdrId(ToolUtil.getUid());
		// 用户ID
		specialDrivingRecord.setUserId(reqRealTimeCalculationDTO.getUserId());
		// 订单ID
		specialDrivingRecord.setOrderId(reqRealTimeCalculationDTO.getOid());
		// 经度
		specialDrivingRecord.setLongitude(reqRealTimeCalculationDTO.getLongitude());
		// 纬度
		specialDrivingRecord.setLatitude(reqRealTimeCalculationDTO.getLatitude());
		// 行驶距离
		specialDrivingRecord.setTravelDistance(realCalculationDTO.getDistance());
		// 金额
		specialDrivingRecord.setAmount(realCalculationDTO.getAmount());
		// 时长
		specialDrivingRecord.setDuration(realCalculationDTO.getDuration());
		// 创建时间
		specialDrivingRecord.setCreateTime(realCalculationDTO.getCreatTime());
		
		return specialDrivingRecordFacade.saveSpecialDrivingRecored(specialDrivingRecord);
	}
	
	/**
	 * 
	 * @Title: getOrderMileageCost 
	 * @Description: 计算订单行驶里程、时长以及费用
	 * @param @return    设定文件 
	 * @author YHQ 
	 * @since V2.5.7
	 * @return ResRealTimeCalculationDTO    返回类型
	 * 
	 */
	public ResRealTimeCalculationDTO getOrderMileageCost(ReqRealTimeCalculationDTO reqRealTimeCalculationDTO){
		
		// 实时计算距离价格响应Bean
		ResRealTimeCalculationDTO resRealTimeCalculationDTO = new ResRealTimeCalculationDTO();
		
		// 获取车辆行车记录最新一条记录
		ReqSpecialDrivingRecord reqSpecialDrivingRecord = new ReqSpecialDrivingRecord();
		
		// 订单Id
		reqSpecialDrivingRecord.setOrderId(reqRealTimeCalculationDTO.getOid());
		// 用户Id
		reqSpecialDrivingRecord.setUserId(reqRealTimeCalculationDTO.getUserId());
		
		// 计算订单行驶里程、时长以及费用
		ResOrderMileageCost resOrderMileageCost = specialDrivingRecordFacade.getOrderMileageCost(reqSpecialDrivingRecord);
		
		if(resOrderMileageCost == null){
			
			resRealTimeCalculationDTO.setAmount(new BigDecimal(0));
			resRealTimeCalculationDTO.setDistance(new BigDecimal(0));
			resRealTimeCalculationDTO.setDuration(new BigDecimal(0));
			
		}else{
			resRealTimeCalculationDTO.setAmount(resOrderMileageCost.getAmount().divide(new BigDecimal(100),1,BigDecimal.ROUND_UP));
			resRealTimeCalculationDTO.setDistance(resOrderMileageCost.getDistance().divide(new BigDecimal(1000),1,BigDecimal.ROUND_UP));
			resRealTimeCalculationDTO.setDuration(resOrderMileageCost.getDuration().divide(new BigDecimal(60),1,BigDecimal.ROUND_UP));
		}
		
		return resRealTimeCalculationDTO;
	}
	
}
